/**
 *
 */
package cn.hg.jfsa.manage.service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.ehcache.CacheKit;

import cn.hg.jfsa.model.JfsaAdmin;
import cn.hg.jfsa.model.JfsaResource;

/**
 * @author Golly
 */
public class JfsaResourceService {

	/**
	 * @param resource
	 */
	public void del (JfsaResource resource) {
		this.del(resource, true);
	}

	/**
	 * @param resource
	 * @param withChildren
	 */
	public void del (JfsaResource resource, boolean withChildren) {

		if (withChildren) {
			this.delWithChildren(resource);
		} else {
			resource.setStatus("del").setChildrenIds(null).update();
		}

	}

	/**
	 * @param admin
	 *
	 * @return
	 */
	public Map<String, JfsaResource> getActionByAdmin (JfsaAdmin admin) {
		Kv para = Kv.by("admin", admin).set("type", "action");
		// 通过SQL模板引擎构建SQL及参数
		SqlPara sqlPara = Db.getSqlPara("system.findResourceByAdminID", para);
		List<JfsaResource> resourceList = JfsaResource.DAO.find(sqlPara);
		sqlPara = Db.getSqlPara("system.findResourceByOrgID", para);
		List<JfsaResource> resourceList2 = JfsaResource.DAO.find(sqlPara);
		HashMap<String, JfsaResource> resourceMap = new HashMap<>();

		for (JfsaResource resource : resourceList) {

			if (StrKit.notBlank(resource.getCode())) {
				resourceMap.put(resource.getCode(), resource);
			}

			if (StrKit.isBlank(resource.getValue())) {
				continue;
			}

			String[] valArray = resource.getValue().split("\n");

			for (String val : valArray) {

				if (val.trim().length() > 0) {
					resourceMap.put(val, resource);
				}

			}

		}

		for (JfsaResource resource : resourceList2) {

			if (StrKit.notBlank(resource.getCode())) {
				resourceMap.put(resource.getCode(), resource);
			}

			if (StrKit.isBlank(resource.getValue())) {
				continue;
			}

			String[] valArray = resource.getValue().split("\n");

			for (String val : valArray) {
				resourceMap.put(val, resource);
			}

		}

		return resourceMap;
	}

	public List<JfsaResource> getMenuTreeByAdmin (JfsaAdmin admin) {
		Kv para = Kv.by("admin", admin).set("type", "menu");
		// 通过SQL模板引擎构建SQL及参数
		SqlPara sqlPara = Db.getSqlPara("system.findResourceByAdminID", para);
		List<JfsaResource> menuList = JfsaResource.DAO.find(sqlPara);
		sqlPara = Db.getSqlPara("system.findResourceByOrgID", para);
		List<JfsaResource> menuList2 = JfsaResource.DAO.find(sqlPara);

		for (JfsaResource menu : menuList2) {

			if (menuList.contains(menu)) {
				continue;
			}

			menuList.add(menu);
		}

		return menuList;
	}

	/**
	 * @return
	 */
	public List<JfsaResource> getTree () {
		List<JfsaResource> tree = CacheKit.get("jfsaResource", "tree");

		if (null == tree) {
			List<JfsaResource> list = JfsaResource.DAO.find("SELECT * FROM jfsa_resource WHERE status=?", "on");
			tree = this.buildTree(list, 0);
			tree.add(0, new JfsaResource().setId(0).setName("-- 顶层 --").setParentId(0).setStatus("on")
				.setType("company").setCreatorId(1).setEditorId(1).setLayerNum(0));
			CacheKit.put("jfsaResource", "tree", tree);
		}

		return tree;
	}

	/**
	 * @param resource
	 */
	public void updateChildredIds (JfsaResource resource) {

		if (null == resource) {
			return;
		}

		String childrenIds = Db.queryFirst(
			"SELECT GROUP_CONCAT(id) FROM jfsa_resource WHERE status<>'del' AND parent_id=?", resource.getId());
		resource.setChildrenIds(childrenIds).update();
	}

	public void updateStatus (JfsaResource resource, String status) {
		this.updateStatusWithChildren(resource, status);
	}

	/**
	 * @param list
	 * @param parentId
	 *
	 * @return
	 */
	private List<JfsaResource> buildTree (final List<JfsaResource> list, Integer parentId) {
		List<JfsaResource> tree = new ArrayList<>();

		for (JfsaResource l : list) {

			if (parentId.equals(l.getParentId())) {
				tree.add(l);
			}

		}

		for (JfsaResource t : tree) {
			t.setChildren(this.buildTree(list, t.getId()));
		}

		tree.sort(new Comparator<JfsaResource>() {

			@Override
			public int compare (JfsaResource o1, JfsaResource o2) {
				return o1.getSortNum().compareTo(o2.getSortNum());
			}

		});
		return tree;
	}

	/**
	 * @param resource
	 */
	private void delWithChildren (JfsaResource resource) {
		resource.setStatus("del").setChildrenIds(null).update();
		List<JfsaResource> childrenList = JfsaResource.DAO
			.find("SELECT * FROM jfsa_resource WHERE status<>'del' AND parent_id = ?", resource.getId());

		for (JfsaResource child : childrenList) {
			child.setStatus("del").setChildrenIds(null).update();
			this.delWithChildren(child);
		}

	}

	/**
	 * @param resource
	 */
	private void updateStatusWithChildren (JfsaResource resource, String status) {
		resource.setStatus(status).update();
		List<JfsaResource> childrenList = JfsaResource.DAO
			.find("SELECT * FROM jfsa_resource WHERE status<>'del' AND parent_id = ?", resource.getId());

		for (JfsaResource child : childrenList) {
			child.setStatus(status).update();
			this.updateStatusWithChildren(child, status);
		}

	}

}
